//
//  ServiceJobsTableViewCell.swift
//  GreenDeal
//
//  Created by Moennig Q on 15/12/2022.
//
import UIKit
import SnapKit
class ServiceJobsTableViewCell: TableViewCell {
    var dateView: PVDsListCellCalendarView!
    var titleLabel: ContentLabel!
    var addresslabel: ContentLabel!
    var amountlabel: ContentLabel!
    lazy var jobStatusLabel: PaddingLabel = {
       let label = PaddingLabel()
        label.paddingTop = 4
        label.paddingLeft = 12
        label.paddingRight = 12
        label.paddingBottom = 4
        label.setFont(R.font.interMedium(size: 13))
        label.textAlignment = .center
        label.layer.cornerRadius = 12
        label.clipsToBounds = true
        return label
    }()
    lazy var paymentStatusLabel: PaddingLabel = {
       let label = PaddingLabel()
        label.paddingTop = 4
        label.paddingLeft = 12
        label.paddingRight = 12
        label.paddingBottom = 4
        label.setFont(R.font.interMedium(size: 12))
        label.textAlignment = .center
        label.layer.cornerRadius = 3
        label.clipsToBounds = true
        label.backgroundColor = .white
        return label
    }()
    //
    override func setUpviews() {
        self.selectionStyle = .none//取消选中效果
        //设置透明颜色
        self.backgroundColor = UIColor(hexStr: Constant.TransparentColorHexStr)
        self.contentView.backgroundColor = UIColor(hexStr: Constant.TransparentColorHexStr)
        //back view
        let backView = CustomBackView()
        backView.backgroundColor = .white
        self.contentView.addSubview(backView)
        backView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview().offset(-16)
        }
        //date view
        dateView = PVDsListCellCalendarView()
        backView.addSubview(dateView)
        dateView.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.left.equalTo(16)
            make.width.height.equalTo(46)
        }
        
        //job status
        backView.addSubview(jobStatusLabel)
        jobStatusLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(dateView)
            make.right.equalTo(-20)
            make.width.height.greaterThanOrEqualTo(0)
            
        }
        //title
        titleLabel = ContentLabel(R.font.interBold(size: 18), hexStr: "#101E30FF")
        titleLabel.textAlignment = .left
        titleLabel.numberOfLines = 0
        backView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(dateView.snp.bottom).offset(18)
            make.left.equalTo(dateView)
            make.right.equalTo(-16)
            make.width.height.greaterThanOrEqualTo(0)
            
        }
        //address
        addresslabel = ContentLabel()
        addresslabel.textAlignment = .left
        addresslabel.numberOfLines = 0
        backView.addSubview(addresslabel)
        addresslabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.left.right.equalTo(titleLabel)
            make.height.greaterThanOrEqualTo(0)
        }
        //payment status
        backView.addSubview(paymentStatusLabel)
        paymentStatusLabel.snp.makeConstraints { (make) in
            make.top.equalTo(addresslabel.snp.bottom).offset(13)
            make.left.equalTo(dateView)
            make.width.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview().offset(-20)
        }
        //amount
        amountlabel = ContentLabel(R.font.interBold(size: 15))
        backView.addSubview(amountlabel)
        amountlabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(paymentStatusLabel)
            make.right.equalTo(jobStatusLabel)
            make.width.height.greaterThanOrEqualTo(0)
        }
    }
    func setData(_ model: ServiceJobListModel){
        var monthStr = ""
        var dayStr = ""
        if model.scheduler_time != "" {
            monthStr = DateUtil().convertDateStrToDateStr(dateStr: model.scheduler_time, oldFormat: "MMM dd yyyy HH:mm:ss", newFormat: "MMM")
            dayStr = DateUtil().convertDateStrToDateStr(dateStr: model.scheduler_time , oldFormat: "MMM dd yyyy HH:mm:ss", newFormat: "dd")
        }else{
            monthStr = DateUtil().convertDateStrToDateStr(dateStr: model.due_date, oldFormat: "MMM dd yyyy", newFormat: "MMM")
            dayStr = DateUtil().convertDateStrToDateStr(dateStr: model.due_date , oldFormat: "MMM dd yyyy", newFormat: "dd")
        }
        self.dateView.setData(monthStr,day: dayStr)
        self.titleLabel.text = model.title
        self.addresslabel.text = model.address
        //
        let jobStatus = model.status
        self.jobStatusLabel.text = jobStatus
        //job status
        switch jobStatus {
        case ServiceJobStatusName.scheduled.rawValue:
            self.jobStatusLabel.setAppearenceColor(textColorHexWithAlpha: "#70798AFF", backColorHexWithAlpha: "#70798A1A")
        case ServiceJobStatusName.started.rawValue:
            self.jobStatusLabel.setAppearenceColor(textColorHexWithAlpha: "#ED9A1DFF", backColorHexWithAlpha: "#ED9A1D1A")
        case ServiceJobStatusName.unqualified.rawValue:
            self.jobStatusLabel.setAppearenceColor(textColorHexWithAlpha: "#DB4444FF", backColorHexWithAlpha: "#DB44441A")
        case ServiceJobStatusName.approved.rawValue:
            self.jobStatusLabel.setAppearenceColor(textColorHexWithAlpha: "#4689DCFF", backColorHexWithAlpha: "#4689DC1A")
        case ServiceJobStatusName.finished.rawValue:
            self.jobStatusLabel.setAppearenceColor(textColorHexWithAlpha: "#46B410FF", backColorHexWithAlpha: "#46B4101A")
        default:
            break
        }
        //budget
        if model.budget != "" && model.budget != "0"{
            //去掉小数点，只显示整数部分
            let realPrice = Tool().formatFloatStrToCurrency(model.budget)
            self.amountlabel.text = "$\(realPrice)"
            if jobStatus == ServiceJobStatusName.finished.rawValue {//finish是绿色
                self.amountlabel.setAppearenceColor(textColorHexWithAlpha: "#379657FF")
            }else{//其他同一个颜色
                self.amountlabel.setAppearenceColor(textColorHexWithAlpha: "#ED9A1DFF")
            }
        }else{
            self.amountlabel.text = ""
        }
        //payment status
        let paymentStatus = model.payment_status
        self.paymentStatusLabel.text = R.string.localizable.paymentStatus() + ": " + paymentStatus
        if jobStatus == ServiceJobStatusName.finished.rawValue{
            self.paymentStatusLabel.setAppearenceColor(textColorHexWithAlpha: "#379657FF",borderColorHexWithAlpha: "#379657FF", cornerRadius: 3)
        }else{
            self.paymentStatusLabel.setAppearenceColor(textColorHexWithAlpha: "#70798AFF",borderColorHexWithAlpha: "#70798AFF", cornerRadius: 3)
        }
    }
}
